package cn.ickck.law_paper_final.controller;


import cn.ickck.law_paper_final.model.entity.DateInitial;
import cn.ickck.law_paper_final.model.entity.User;
import cn.ickck.law_paper_final.service.UserService;
import cn.ickck.law_paper_final.utils.JsonData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * @Author: ck
 * @Description:
 * @Date: Create in 15:03 2020/10/17
 */

@RestController
@RequestMapping("/api/pub/v2/user")
public class UserController {
    @Autowired
    private UserService userService;
    @RequestMapping("login")
    public JsonData login(@RequestBody User user)
    {
        User user1  = userService.findByName(user.getUsername(),user.getPassword());
        return user1!=null?JsonData.buildSuccess("登陆成功"):JsonData.buildError("登陆失败");
    }


    @RequestMapping("test")
    public void test(@RequestBody DateInitial date)
    {
        System.out.println(date.getDate());
        System.out.println(date.getName());

    }




    /*
    事务开启测试
     */
    @Transactional
    @RequestMapping("register")
    public JsonData register(@RequestBody User user)
    {
        if(user.getUsername()==null||user.getPassword()==null)
        {
            return JsonData.buildError("用户名或密码不能为空");
        }
        User user1  = userService.findByName(user.getUsername(),user.getPassword());
        if(user1!= null)
        {
            return JsonData.buildError("账号已注册");
        }

        userService.insert(user);

//        int i = 1/0;
        return JsonData.buildSuccess("注册成功");
    }
}
